#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import threading
import time
import traceback

import pyautogui
from selenium.webdriver.common.by import By
from time import sleep
from base.ObjectMap import ObjectMap
from base.PrintRemitLoginBase import PrintRemitLoginBase
from logs.log import log


class PrintRemitLoginPage(PrintRemitLoginBase, ObjectMap):
    def login_input_value(self, pwd):
        sleep(2)
        print("输入密码")
        length = len(pwd)
        for i in range(length):
            print(pwd[i])
            pyautogui.press(pwd[i])
        # 敲击回车键
        print("回车")
        pyautogui.press('enter')

    def login_click(self, driver):
        log.info("点击登录")
        button_xpath = self.login_button()
        return self.element_click(driver, By.XPATH, button_xpath)

    def to_login(self, driver, url, pwd):
        log.info("跳转登录页面")
        self.element_to_url(driver, url)
        self.mouse_thread = threading.Thread(target=self.login_input_value, kwargs={"pwd": pwd})
        self.mouse_thread.start()
        self.login_click(driver)
        sleep(1)
        # self.login_input_value(pwd)
        # sleep(1)

    def to_print(self, driver, data, pwd):
        # @TODO 选择登录用户
        # self.element_click(driver, By.XPATH, "")
        # 预算执行
        # self.element_click(driver, By.XPATH, '//*[@id="pay"]')

        url = driver.current_url
        query = url.split('?')[1]
        new_url = f"https://59.255.67.8/pay/index.html?{query}&appguid=pay#/HomeCard"
        print(f"打开新页面：${new_url}")
        self.element_to_url(driver, new_url)

        time.sleep(0.5)

        # 菜单
        self.element_click(driver, By.XPATH, '//span[text()="菜单"]')
        # 预算执行
        self.element_click(driver, By.XPATH, '//span[contains(text(),"预算执行")]/../../i')
        # 部门支出预算执行
        self.element_click(driver, By.XPATH,
                           '//span[contains(text(),"部门支出预算执行")]/../../i')
        # 国库集中支付
        self.element_click(driver, By.XPATH,
                           '//span[contains(text(),"国库集中支付")]/../../i')
        # # 资金支付
        self.element_click(driver, By.XPATH,
                           '//span[contains(text(),"资金支付") and not(contains(text(),"资金支付（内网）"))]/../../i')
        # # 集中支付申请录入（预算单位）
        self.element_click(driver, By.XPATH,
                           '//span[contains(text(),"集中支付申请录入（预算单位）")]')
        # 公务卡支付申请生成（集中支付）
        # self.element_click(driver, By.XPATH,
        #                    '//*[@id="quickNavAll"]/div/div/div[1]/div/div[1]/div/ul/div/li/ul/div[3]/li/ul/div[3]/li/ul/li[2]/div')
        time.sleep(2.5)
        i=0
        for person in data:
            i = i+1
            person['images'] = []
            try:
                if i == 1:
                    for i in range(8):
                        self.right_click(driver, By.XPATH,
                                         '//*[@id="UnitPaymentApplication-2C27DDF8B6A7688077BA81F9F37EE5EB-指标额度"]/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[5]')
                        # 隐藏列
                        self.element_click(driver, By.XPATH,
                                           "//span[text()='隐藏列']")
                        # 隐藏当前列
                        self.element_click(driver, By.XPATH,
                               "//span[text()='隐藏当前列']")

                    # 项目名称
                    self.element_click(driver, By.XPATH,
                                       '//span[text()="项目名称"]/../span/i[@title="对所选的列启用筛选"]')
                    # # 输入内容
                    self.element_fill_value1(driver, By.XPATH,
                                             "//input[@placeholder='输入关键字过滤']", person['项目名称'], 30)
                    time.sleep(1)
                    self.element_click(driver, By.XPATH,
                                       "//span[text()='筛选']")
                    #点击+号
                    self.element_click(driver, By.XPATH,
                                       '//*[@id="UnitPaymentApplication-2C27DDF8B6A7688077BA81F9F37EE5EB-指标额度"]/div/div[1]/div[2]/div[2]/div[2]/table/tbody/tr/td[3]/div/div/div[2]/div/span')


                    # 项目名称
                    self.element_click(driver, By.XPATH,
                                       '//span[text()="是否政府采购"]/../span/i[@title="对所选的列启用筛选"]')
                    # # 输入内容
                    self.element_fill_value1(driver, By.XPATH,
                                             "//input[@placeholder='输入关键字过滤']","否", 30) # person['是否政府采购']
                    time.sleep(1)
                    self.element_click(driver, By.XPATH,
                                       "//span[text()='筛选']")
                    # 指标类型
                    self.element_click(driver, By.XPATH,
                                       '//span[text()="指标类型"]/../span/i[@title="对所选的列启用筛选"]')
                    # # 输入内容
                    self.element_fill_value1(driver, By.XPATH,
                                             "//input[@placeholder='输入关键字过滤']", person['指标类型'], 30)
                    time.sleep(1)
                    self.element_click(driver, By.XPATH,
                                       "//span[text()='筛选']")
                    # 项目名称
                    self.element_click(driver, By.XPATH,
                                       '//span[text()="支出功能分类科目"]/../span/i[@title="对所选的列启用筛选"]')
                    # # 输入内容
                    self.element_fill_value1(driver, By.XPATH,
                                             "//input[@placeholder='输入关键字过滤']", person['支出功能分类科目'], 30)
                    time.sleep(1)
                    self.element_click(driver, By.XPATH,
                                       "//span[text()='筛选']")
                    # 指标类型
                    self.element_click(driver, By.XPATH,
                                       '//span[text()="政府支出经济分类"]/../span/i[@title="对所选的列启用筛选"]')
                    # # 输入内容
                    self.element_fill_value1(driver, By.XPATH,
                                             "//input[@placeholder='输入关键字过滤']", person['政府支出经济分类'], 30)
                    time.sleep(1)
                    self.element_click(driver, By.XPATH,
                                       "//span[text()='筛选']")
                    # 选择项目单选框
                    self.element_click(driver, By.XPATH,
                                       '//*[@id="UnitPaymentApplication-2C27DDF8B6A7688077BA81F9F37EE5EB-指标额度"]/div/div[1]/div[2]/div[4]/div[2]/table/tbody/tr/td/div/a')
                    time.sleep(2)
                    # 选择收款人
                    self.element_click(driver, By.XPATH,
                                       '//div[@class="general_pay"]/div/form/div[1]/div[2]/div/div[2]/div[1]/div[2]/i')
                    time.sleep(1)
                    # 输入收款人名称
                    self.element_fill_value1(driver, By.XPATH,
                                             '//input[@placeholder="收款人名称"]',
                                             person['收款人名称'], 30)
                    # 输入收款人账号
                    self.element_fill_value1(driver, By.XPATH,
                                             '//input[@placeholder="收款人账号"]',
                                             person['收款人账号'], 30)
                    # 输入开户行
                    # self.element_fill_value1(driver, By.XPATH,
                    #                          '//input[@placeholder="开户行"]',
                    #                          person['开户行总行'], 30)
                    time.sleep(1)
                    # 点击搜索  有问题
                    self.element_click(driver, By.XPATH,
                                       '//div[@class="basic-search-form-main mgt10"]/div/div/div[2]/button[1]')
                    time.sleep(2)
                    rows = len(self.find_element_rows(driver, By.XPATH,
                                                      '//div[@class="vxe-table tid_249 border--full vxe-editable show--head show--foot has--height fixed--left t--animat t--selected row--highlight"]/div[2]/div[2]/table/tbody/tr'))
                    print(rows)
                    time.sleep(3)
                    if rows < 1:
                        # 新增收款人信息
                        # 点击增加
                        self.element_click(driver, By.XPATH,
                                           '//button/span[text()="增加"]')
                        # 收款人全称
                        self.element_fill_value1(driver, By.XPATH,
                                                 '//input[@placeholder="收款人全称"]',
                                                 person['收款人名称'], 30)
                        # 收款人账号
                        self.element_fill_value1(driver, By.XPATH,
                                                 '//div[@class="mainConter"]/div/form/div[2]/div/div[2]/div/input',
                                                 person['收款人账号'], 30)
                        #请选择收款人银行/行别
                        self.element_click(driver, By.XPATH,
                                           '//input[@placeholder="请选择收款人银行/行别"]')
                        time.sleep(1)
                        self.element_fill_value1(driver, By.XPATH,
                                                 '//input[@placeholder="输入关键字进行过滤"]', person['开户行总行'], 30)
                        time.sleep(1)
                        # //span[text()' + '"' + person['开户行总行'] + '"' + ']
                        self.double_click(driver, By.XPATH, '//span[contains(text(),'+'"' + person['开户行总行'] + '"'+')]')
                        time.sleep(1.5)
                        link_position = (915, 450)
                        # 然后模拟鼠标移动到链接位置
                        pyautogui.moveTo(link_position, duration=1)
                        # 模拟单次点击
                        pyautogui.click(clicks=1, interval=1)
                        # 请选择
                        self.element_click(driver, By.XPATH,
                                           '//div[@class="mainConter"]/div/form/div[6]/div/div[2]/div/div/div/div/div/div/div/span/span/i')
                                                                                # div[6]/div/div[2]/div/div/div/div/div/div/div/span/span/i
                        # /html/body/div[6]/div[1]/div[2]/div[1]/div/div[1]/div[3]/div/div[2]/div[2]/div[4]/div[3]/div/div[2]/div[1]/div/div[1]/div/form/div[6]/div/div[2]/div/div/div/div/div[1]/div/div/input
                        #103142790248
                        time.sleep(2)
                        self.element_fill_value1(driver, By.XPATH,
                                                 '/html/body/div[13]/div/div[1]/div[1]/div/input',person['开户行行号'], 80)
                        # /html/body/div[14]/div/div[1]/div[1]/div/input
                        # vxe-table--ignore-clear vxe-pulldown--panel size--medium is--transfer
                        # //div[@class="option unit-tree-main-drop-option no-filter"]/div/div[1]/div[1]/input
                        # /html/body/div[13]/div/div[1]/div[1]/div/input
                        #//div[@class="vxe-table--ignore-clear vxe-pulldown--panel size--medium is--transfer"]/div/div/div/div/input
                        time.sleep(3)
                        pyautogui.press('enter')
                        time.sleep(1.5)
                        # //span[text()' + '"' + person['开户行总行'] + '"' + ']
                        # self.double_click(driver, By.XPATH, '//span[contains(text(),'+'"' + person['开户行行号'] + '"'+')]')
                        self.element_click(driver, By.XPATH,'//div[@class="main"]/div[2]/div/button[1]')
                        # 选中第一条
                        self.element_click(driver, By.XPATH,
                                           '//div[@class="vxe-table tid_249 border--full vxe-editable show--head show--foot has--height fixed--left t--animat t--selected row--highlight"]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/span/span[2]')

                    else:
                        # 选中第一条
                        self.element_click(driver, By.XPATH,
                                           '//div[@class="vxe-table tid_249 border--full vxe-editable show--head show--foot has--height fixed--left t--animat t--selected row--highlight"]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/span/span[2]')
                                                     # /html/body/div[7]/div[1]/div[2]/div[1]/div/div[1]/div[3]/div/div[2]/div[2]/div[4]/div[1]/div/div[2]/div[1]/div/div[3]/div[1]/div/div[1]/div[1]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/span/span[2]
                    # 点击确定
                    self.element_click(driver, By.XPATH,
                                       '//button/span[text()="确定"]')
                    time.sleep(2)
                    #部门经济分类
                    self.element_click(driver, By.XPATH,
                                       '//div[@class="general_pay"]/div/form/div[1]/div[7]/div/div[2]/div/div/div/div/div/div/div/div[1]/div/div/input')
                    time.sleep(1)
                    self.element_fill_value1(driver, By.XPATH,
                                             '//input[@placeholder="输入关键字进行过滤"]', person['部门经济分类'], 30)
                    time.sleep(1)
                    self.double_click(driver, By.XPATH, '//span[text()=' + '"' + person['部门经济分类'] + '"' + ']')
                    time.sleep(2)
                    # 先定位链接的位置
                    link_position = (915, 450)
                    # 然后模拟鼠标移动到链接位置
                    pyautogui.moveTo(link_position, duration=1)
                    # 模拟单次点击
                    pyautogui.click(clicks=1, interval=1)
                    # # 等待页面加载完成
                    time.sleep(2)
                    print("开始滚动")
                    for i in range(4):
                        self.roll_click(driver, By.XPATH,
                                        '/html/body/div[6]/div[1]/div[2]/div[1]/div/div[1]')
                    print("结束滚动")
                    time.sleep(4)
                    # 资金往来对象
                    # self.element_click(driver, By.XPATH,
                    #                    '//input[@placeholder="请选择资金往来对象"]/../span/span/i')
                    # # /html/body/div[7]/div[1]/div[2]/div[1]/div/div[1]/div[3]/div/div[2]/div[2]/div[2]/div/form/div[1]/div[9]/div/div[2]/div/div/div/div/div/div/div/div/div/div/span/span/i
                    # time.sleep(1)
                    # self.element_fill_value1(driver, By.XPATH,
                    #                          '//input[@placeholder="输入关键字进行过滤"]', person['资金往来对象'], 30)
                    # time.sleep(1)
                    # # "//span[text()="+person['资金往来对象']+"]"
                    # self.double_click(driver, By.XPATH, '//span[text()=' + '"' + person['资金往来对象'] + '"' + ']')
                    # time.sleep(1)
                    # 资金用途 //input[@placeholder="请输入"]
                    self.element_fill_value1(driver, By.XPATH,
                                             '//div[@class="general_pay"]/div/form/div[1]/div[11]/div/div[2]/div/div/div/div/input',
                                             person['资金用途'], 30)
                    time.sleep(5)
                    self.element_click(driver, By.XPATH,
                                       '//div[@class="general_pay"]/div/form/div[1]/div[14]/div/div[1]/span/span')
                    # 申请金额 //input[@placeholder="请输入申请金额"]
                    self.element_fill_value1(driver, By.XPATH,
                                             '//div[@class="general_pay"]/div/form/div[1]/div[13]/div/div[2]/div/div/div/div/input',
                                             person['申请金额'], 30)
                    time.sleep(1)
                    self.element_fill_value1(driver, By.XPATH,
                                             '//div[@class="general_pay"]/div/form/div[1]/div[15]/div/div[2]/div/div/div/div/input',
                                             person['附言'], 30)
                    time.sleep(1)
                    self.element_fill_value1(driver, By.XPATH,
                                             '//div[@class="general_pay"]/div/form/div[1]/div[16]/div/div[2]/div/div/div/div/textarea',
                                             person['摘要'], 30)

                    time.sleep(5)
                    # 保存并送审（按钮）
                    # self.element_click(driver, By.XPATH,
                    #                    '//button/span[text()="保存并送审"]')
                    # time.sleep(15)
                    # # 先定位链接的位置
                    # link_position = (750, 390)
                    # # 然后模拟鼠标移动到链接位置
                    # pyautogui.moveTo(link_position, duration=1)
                    # # 模拟单次点击
                    # pyautogui.click(clicks=1, interval=1)
                    # # # 等待页面加载完成
                    # time.sleep(5)
                    # length = len(pwd)
                    # for i in range(length):
                    #     print(pwd[i])
                    #     pyautogui.press(pwd[i])
                    # # 敲击回车键
                    # print("回车")
                    # pyautogui.press('enter')
                    # time.sleep(2)
                    person['success'] = True
                    person['message'] = ""
                    person['images'].append(self.save_screenshot1(driver))
            except BaseException as e:
                log.error(traceback.format_exc())
                person['success'] = False
                person['message'] = str(e)
                person['images'].append(self.save_screenshot1(driver))
                pass
            # else:
            #     log.error(traceback.format_exc())
            #     person['success'] = False
            #     person['message'] = traceback.format_exc()
            #     pass
        return data
